"""
贪吃蛇

Version: 0.1
Author: yuwl
Date：2024-3-31
"""

import tkinter as tk
import random
import time

# 设置游戏窗口大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
# 设置蛇的初始大小和速度
SNAKE_SIZE = 20
SNAKE_SPEED = 20

# 蛇类
class Snake:
    def __init__(self):
        self.length = 1
        self.positions = [(0, 0)]
        self.direction = random.choice(['Left', 'Right', 'Up', 'Down'])
        self.color = 'green'

    def turn(self, event):
        new_direction = event.keysym
        if new_direction in ['Left', 'Right', 'Up', 'Down']:
            self.direction = new_direction

    def move(self):
        cur = self.positions[0]
        x, y = cur
        if self.direction == 'Left':
            x -= SNAKE_SIZE
        elif self.direction == 'Right':
            x += SNAKE_SIZE
        elif self.direction == 'Up':
            y -= SNAKE_SIZE
        elif self.direction == 'Down':
            y += SNAKE_SIZE
        self.positions.insert(0, (x, y))
        if len(self.positions) > self.length:
            self.positions.pop()

    def draw(self, canvas):
        for p in self.positions:
            canvas.create_rectangle(p, (p[0] + SNAKE_SIZE, p[1] + SNAKE_SIZE), fill=self.color)

# 食物类
class Food:
    def __init__(self):
        self.position = (0, 0)
        self.color = 'red'
        self.randomize_position()

    def randomize_position(self):
        x = random.randint(0, (WINDOW_WIDTH - SNAKE_SIZE) // SNAKE_SIZE) * SNAKE_SIZE
        y = random.randint(0, (WINDOW_HEIGHT - SNAKE_SIZE) // SNAKE_SIZE) * SNAKE_SIZE
        self.position = (x, y)

    def draw(self, canvas):
        canvas.create_oval(self.position, (self.position[0] + SNAKE_SIZE, self.position[1] + SNAKE_SIZE), fill=self.color)

# 游戏类
class Game:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("Snake Game")
        self.canvas = tk.Canvas(self.window, width=WINDOW_WIDTH, height=WINDOW_HEIGHT, bg='white')
        self.canvas.pack()
        self.snake = Snake()
        self.food = Food()
        self.window.bind('<Key>', self.snake.turn)
        self.score = 0
        self.game_over = False

    def run(self):
        while not self.game_over:
            self.update()
            self.draw()
            self.window.update_idletasks()
            self.window.update()
            time.sleep(0.1)

    def update(self):
        if self.snake.positions[0] == self.food.position:
            self.snake.length += 1
            self.score += 1
            self.food.randomize_position()
        self.snake.move()
        if self.snake.positions[0] in self.snake.positions[1:]:
            self.game_over = True
        if self.snake.positions[0][0] < 0 or self.snake.positions[0][0] >= WINDOW_WIDTH:
            self.game_over = True
        if self.snake.positions[0][1] < 0 or self.snake.positions[0][1] >= WINDOW_HEIGHT:
            self.game_over = True

    def draw(self):
        self.snake.draw(self.canvas)
        self.food.draw(self.canvas)
        self.canvas.create_text(WINDOW_WIDTH - 50, 20, text=f"Score: {self.score}", font=('Arial', 20), fill='black')

    def display_game_over(self):
        self.canvas.create_text(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, text="Game Over", font=('Arial', 40), fill='red')

# 运行游戏
game = Game()
game.run()
game.display_game_over()
